﻿/*
 *author:keily ,department 2,wuda geo
 *create date:2013-1-28
 *description:集合类，继承IDictionary、IGeoSet<T>的接口，增加了求交集、并集、补集的功能
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace GeoWeb.Common
{
    /// <summary>
    /// 集合类，继承IDictionary、IGeoSet<T>的接口，增加了求交集、并集、补集的功能
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DictionarySet<T>:IDictionary<T,T>,IGeoSet<T>
    {
        //字典变量
        private IDictionary<T, T> _map;
        //构造函数，初始化字典
        public DictionarySet()
        { 
            _map=new Dictionary<T,T>();
        }

        #region IDictionary<T,T> 成员
        /// <summary>
        /// 添加元素
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void Add(T key, T value)
        {
            _map.Add(key, value);
        }
        /// <summary>
        /// 判断是否包含指定键的元素
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsKey(T key)
        {
            return _map.ContainsKey(key);
        }
        /// <summary>
        /// 返回所有的建
        /// </summary>
        public ICollection<T> Keys
        {
            get { return _map.Keys; }
        }
        /// <summary>
        /// 移除指定键
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(T key)
        {
            if (_map.ContainsKey(key))
            {
                _map.Remove(key);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取指定键的值，失败为默认值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValue(T key, out T value)
        {
            value=default(T);
            if (_map.ContainsKey(key))
            {
                value = _map[key];
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取集合的所有值
        /// </summary>
        public ICollection<T> Values
        {
            get { return _map.Values; }
        }
        /// <summary>
        /// 按照集合键下标访问
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T this[T key]
        {
            get
            {
                return _map[key];
            }
            set
            {
                _map[key] = value;
            }
        }

        #endregion

        #region ICollection<KeyValuePair<T,T>> 成员
        /// <summary>
        /// 添加一个键值对
        /// </summary>
        /// <param name="item"></param>
        public void Add(KeyValuePair<T, T> item)
        {
            _map.Add(item);
        }
        /// <summary>
        /// 清除集合
        /// </summary>
        public void Clear()
        {
            _map.Clear();
        }
        /// <summary>
        /// 确定集合是否包含键值对
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(KeyValuePair<T, T> item)
        {
            return _map.Contains(item);
        }
        /// <summary>
        /// 从指定键值对开始，将集合的元素拷贝到一个数组中
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(KeyValuePair<T, T>[] array, int arrayIndex)
        {
            this._map.CopyTo(array, arrayIndex);
        }
        /// <summary>
        /// 获取集合的大小
        /// </summary>
        public int Count
        {
            get { return _map.Count; }
        }
        /// <summary>
        /// 集合是否只读
        /// </summary>
        public bool IsReadOnly
        {
            get { return true; }
        }
        /// <summary>
        /// 移除指定集合
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(KeyValuePair<T, T> item)
        {
            return _map.Remove(item);
        }

        #endregion

        #region IEnumerable<KeyValuePair<T,T>> 成员
        /// <summary>
        /// 返回一个循环访问集合的枚举器
        /// </summary>
        /// <returns></returns>
        public IEnumerator<KeyValuePair<T, T>> GetEnumerator()
        {
            return _map.GetEnumerator();
        }

        #endregion

        #region IEnumerable 成员
        /// <summary>
        /// 返回一个循环访问集合的枚举器
        /// </summary>
        /// <returns></returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _map.GetEnumerator();
        }

        #endregion

        #region ISet<T> Members
        /// <summary>
        /// 获取2个集合的并集
        /// </summary>
        /// <param name="other">集合B</param>
        /// <returns></returns>
        public IGeoSet<T> Union(IGeoSet<T> other)
        {
            return GeoSet<T>.Union(this, other);
        }

        /// <summary>
        /// 获取交集
        /// </summary>
        /// <param name="other">集合B</param>
        /// <returns></returns>
        public IGeoSet<T> Intersect(IGeoSet<T> other)
        {
            return GeoSet<T>.Intersect(this, other);
        }


        /// <summary>
        /// 获取一个集合，此集合不包含集合A、集合B的并集
        /// </summary>
        /// <param name="other">集合B</param>
        /// <returns></returns>
        public IGeoSet<T> ExclusiveOr(IGeoSet<T> other)
        {
            return GeoSet<T>.ExclusiveOr(this, other);
        }


        /// <summary>
        /// 获取集合A与集合B交集的补集
        /// </summary>
        /// <param name="other">集合B</param>
        /// <returns></returns>
        public IGeoSet<T> Minus(IGeoSet<T> other)
        {
            return GeoSet<T>.Minus(this, other);
        }
        #endregion

        #region ICollection<T> Members
        /// <summary>
        /// 增加元素到集合中
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            _map.Add(item, item);
        }


        /// <summary>
        /// 确定集合是否包含元素
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(T item)
        {
            return _map.ContainsKey(item);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        [Obsolete("此方法未重写，建议不要使用")]
        public void CopyTo(T[] array, int arrayIndex)
        {

        }
        #endregion

        #region IEnumerable<T> Members
        /// <summary>
        /// 返回一个循环访问集合的枚举器，此方法继承了collection的接口
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return _map.Keys.GetEnumerator();
        }
        #endregion
    }
}
